package com.tina.listener;

import com.alibaba.fastjson.JSON;
import com.tina.constant.ShopCode;
import com.tina.entity.Orders;
import com.tina.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Author tina
 * @Time 2020/11/23 上午9:57
 * @Version 1.0
 * @Content
 **/
@Slf4j
@Component
@RocketMQMessageListener(topic = "orderTopic", consumerGroup = "order_orderTopic_cancel_group", messageModel = MessageModel.BROADCASTING)
public class CancelMQListener implements RocketMQListener<MessageExt> {

    @Autowired
    private IOrdersService ordersService;

    @Override
    public void onMessage(MessageExt message) {
        try {
            String body = new String(message.getBody(), "UTF-8");
            log.info("body->{}", body);
            Orders orders = JSON.parseObject(body, Orders.class);

            Orders o = new Orders();
            o.setOrderId(orders.getOrderId());
            o.setStatus(ShopCode.SHOP_ORDER_CANCEL.getCode());
            ordersService.editOrders(o);
            log.info("订单状态设置为取消成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.info("订单状态设置为取消失败");
        }

    }
}
